/*
	Authors: Samuel Thacker, Faiz Rahman, Andy Wu
	Description: A program file which defines the 'format' function.
	Revisions:
		2012/2/06 - changed the checks for NULL to checks for "/0"
		2012/2/02 - added if-else statement in while loop to check if one or more of strings are NULL
		2012/1/30 - Modification to the output.
		2012/1/26 - Creation of this program file.
*/

#include <stdio.h>
#include "format.h"

void format(dll * l,char * fname) /*Takes a linked list and creates a file using it.*/
	{
	de * ele;
	FILE * ofile;
	ele = l->first;

	ofile = fopen(fname,"w");	

	while(ele) /*Ends when ele is null (end of the list).*/
		{
		if((ele->str2[0] == '\0'))
			{
			fprintf(ofile,"%s\n",ele->str1);
			}
		else if(ele->str3[0] == '\0')
			{
			fprintf(ofile,"%s %s\n",ele->str1,ele->str2);
			}
		else if(ele->str4[0] == '\0')
			{
			fprintf(ofile,"%s %s, %s\n",ele->str1,ele->str2,ele->str3);
			}
		else
			{
			fprintf(ofile,"%s %s, %s, %s\n",ele->str1,ele->str2,ele->str3,ele->str4);
			}
		ele = ele->next;
		}

	fclose(ofile);
	}

/*EOF*/
